/*
 * @Author: zjj 
 * @Date: 2018-05-02 13:42:57 
 * @Last Modified by: mikey.zhaopeng
 * @Last Modified time: 2018-05-26 15:05:59
 */
// 引入模块
import gulp from 'gulp'
import optimist from 'optimist'
import path from 'path'
import gulpLoad from 'gulp-load-plugins'
import pngquant from 'imagemin-pngquant'
import del from 'del'
import runSequence from 'run-sequence'

// 引入gulp插件
const $ = gulpLoad();
// 参数获取
const argv = optimist.argv;
// 项目根路径
const basePath = path.resolve(__dirname, '../');
// 项目开发路径
const proPath = path.join(basePath, 'src', 'pcb');
//输出项目地址（dist）
const distPath = path.join(basePath, 'dist');

// ------------------------------dev task --------------------------------//
// scss编译输出css
gulp.task('scss', cd => (
  gulp.src(path.join(proPath, 'scss', '*.scss'))
    .pipe($.plumber())
    // expanded   compressed
    .pipe($.sass({outputStyle: 'compressed'}).on('error', $.sass.logError))
    .pipe($.autoprefixer({browsers: ['last 20 versions']}))
    .pipe(gulp.dest(path.join(proPath, 'css')))
    .pipe($.notify("Found file: <%= file.relative %>!"))
    .on("error", $.notify.onError(function (error) {
      return "Message to the notifier: " + error.message;
    }))
));

// 编译index.js
gulp.task('babel', () =>(
  gulp.src([path.join(proPath, 'js', 'index.js')])
    .pipe($.plumber())
    .pipe($.babel({
      presets: ['env']
    }))
    // .pipe($.uglify())
    .pipe($.rename('main.js'))
    .pipe(gulp.dest(path.join(proPath, 'js')))
    .pipe($.notify("Found file: <%= file.relative %>!"))
    .on("error", $.notify.onError(function (error) {
      return "Message to the notifier: " + error.message;
    }))
));

// ---------------------------build task ------------------------------//
// 清理dist文件
gulp.task('clear', cd => (
  del([distPath], {
    force: true
  })
));

//  复制压缩html文件保存dist
gulp.task('copyhtml', () =>(
  gulp.src([path.join(proPath, '*.html')])
    .pipe($.plumber())
    .pipe($.htmlMinify())
    .pipe(gulp.dest(path.join(distPath)))
    .pipe($.notify("Found file: <%= file.relative %>!"))
    .on("error", $.notify.onError(function (error) {
      return "Message to the notifier: " + error.message;
    }))
));

// 生产css md5文件保存dist
gulp.task('cssmd5', cd => (
  gulp.src(path.join(proPath, 'css', '*.css'))
    .pipe($.plumber())
    .pipe($.revReplace())
    .pipe($.md5Plus(10, path.join(distPath, '*.html')))
    .pipe(gulp.dest(path.join(distPath, 'css')))
    .pipe($.notify("Found file: <%= file.relative %>!"))
    .on("error", $.notify.onError(function (error) {
      return "Message to the notifier: " + error.message;
    }))
));

// 生成图片md5压缩保存dist
gulp.task('imgmd5', cd => (
  gulp.src(path.join(proPath, 'images', '**', '*.{png,jpg,gif,ico}'))
    .pipe($.plumber())
    .pipe($.revReplace())
    .pipe($.cache($.imagemin({
      progressive: true,
      svgoPlugins: [{removeViewBox: false}],
      use: [pngquant()]
    })))
    // .pipe($.md5Plus(10, [path.join(distPath,'*.html'), path.join(distPath,'css', '*.css'), path.join(distPath,'js', '*.js')], {
    //   dirLevel : 1,
    //   mappingFile: 'manifest.json'
    // }))
    .pipe(gulp.dest(path.join(distPath, 'images')))
    .pipe($.notify("Found file: <%= file.relative %>!"))
    .on("error", $.notify.onError(function (error) {
      return "Message to the notifier: " + error.message;
    }))
));

// 复制音视频文件
gulp.task('copymedia', () =>(
  gulp.src([path.join(proPath, 'media', '**', '**')])
  .pipe($.plumber())
  .pipe(gulp.dest(path.join(distPath, 'media')))
  .pipe($.notify("Found file: <%= file.relative %>!"))
    .on("error", $.notify.onError(function (error) {
      return "Message to the notifier: " + error.message;
    }))
));

// 复制js
gulp.task('copyjs', () =>(
  gulp.src([path.join(proPath, 'js', '**', '**'), `!${path.join(proPath, 'js', 'index.js')}`])
  .pipe($.plumber())
  .pipe(gulp.dest(path.join(distPath, 'js')))
  .pipe($.notify("Found file: <%= file.relative %>!"))
    .on("error", $.notify.onError(function (error) {
      return "Message to the notifier: " + error.message;
    }))
));

// 生成jsmd5压缩保存idst
gulp.task('mainjs', () =>(
  gulp.src([path.join(proPath, 'js', '**', '**'), `!${path.join(proPath, 'js', 'index.js')}`])
  .pipe($.plumber())
  .pipe($.uglify())
  .pipe($.md5Plus(10, path.join(distPath, '*.html')))
  .pipe(gulp.dest(path.join(distPath, 'js')))
  .pipe($.notify("Found file: <%= file.relative %>!"))
    .on("error", $.notify.onError(function (error) {
      return "Message to the notifier: " + error.message;
    }))
));

console.log($)

// 监听文件变化
gulp.task('watch', function(){
  gulp.watch(path.join(proPath, 'scss', '*.scss'), ['scss']);
  gulp.watch(path.join(proPath, 'js', 'index.js'), ['babel']);
});


// 生成线上目录
// 顺序执行[clear, scss, babel, copyhtml, cssMd5, imgmd5]
gulp.task('build', () => {
  runSequence(
    'clear',
    ['scss', 'babel'],
    'copyhtml',
    ['mainjs', 'cssmd5', 'copymedia'],
    'imgmd5'
  )
});

gulp.task('default', argv.watch && ['watch']);